#
# Replication file for 
# "How Experience Increases the Legitimacy of Citizen Deliberation: Evidence from Honduras"
# Political Behavior 
# Eric Kramon
# December 8, 2025


# ============================================================================ #
# Load Packages ####

library(tidyverse)
library(haven)
library(ggplot2)
library(estimatr) ## lm_robust
library(lemon)
library(patchwork)
library(forcats)
library(DiagrammeR)
library(DiagrammeRsvg)
library(rsvg)


# ============================================================================ #
# Data ####

dat <- read_csv("pb-replication-kramon.csv")


# ============================================================================ #
# Treatment Variables and Labels ####

dat <- dat %>% 
  mutate(
    experience_treatment = ifelse(discussion == 1, 
                                  "Deliberation Experience Treatment", 
                                  "No Experience (control)"),
    survey_experiment = ifelse(delib_treatment == 1, "Deliberative Citizens' Assembly (survey exp)", 
                               "Status Quo (control survey exp)")
  )


# ============================================================================ #
# Figure 1 Consort Diagram ####

gr <- grViz("
digraph consort {
  graph [rankdir=LR, splines=ortho, nodesep=0.9, ranksep=1.2, margin=.2]
  node  [shape=box, style=\"rounded\", fontsize=12, fontname=Helvetica,
         color=\"#555555\", width=3.2]
  edge  [color=\"#555555\", arrowsize=0.7]

  // Left side
  a [label=<<B>Recruitment + Baseline<br/>Survey</B><BR/>N=1,054<BR/>10/9/21–11/6/21>, width=3.4];
  b [label=<<B>Agreed to participate</B><BR/>Random assignment to<BR/>Deliberation Experience or Control<BR/>N=607>, width=3.6];

  // Middle: groups
  c [label=<<B>Deliberation Experience Treatment<br/> (actual)</B><BR/>N=115<BR/>24 Groups<BR/>11/14/21–12/31/21>, width=3.8];
  d [label=<<B>Control (actual)</B><BR/>N=136<BR/>21 Groups<BR/>11/14/21–12/31/21>, width=3.8];

  {rank=same; c d}

  // Right: four survey-experiment arms
  e1 [label=<<B>Deliberative Citizens Assembly<br/>Survey Experiment</B><BR/>N = 53>, width=3.8];
  e2 [label=<<B>Status Quo<br/>Survey Experiment</B><BR/>N = 62>, width=4.2];

  e3 [label=<<B>Deliberative Citizens Assembly<br/>Survey Experiment</B><BR/>N = 66>, width=3.8];
  e4 [label=<<B>Status Quo<br/>Survey Experiment</B><BR/>N = 70>, width=4.2];

  {rank=same; e2 e3}   // middle pair on same row

  // Edges
  a -> b;
  b -> c;
  b -> d;

  c -> e1;
  c -> e2;

  d -> e3;
  d -> e4;
}
")

gr

# Convert the DiagrammeR object to SVG
svg <- export_svg(gr)

# Save
rsvg_png(charToRaw(svg), 
         file = "figure-1.png",
         width = 6500, height = 8000)   

rsvg_pdf(charToRaw(svg),
         file = "figure-1.pdf")


# ============================================================================ #
# Figure 2  ####


d1 <- dat %>% 
  group_by(discussion) %>%
  filter(survey_experiment  == "Deliberative Citizens' Assembly (survey exp)") %>%
  do(tidy(lm_robust(dem_d ~ 1, data = .))) %>% 
  filter(discussion == 0 | discussion ==1 ) %>% 
  mutate(outcome = "How Democratic")

## how fair
d2 <- dat %>% 
  group_by(discussion) %>%
  filter(survey_experiment  == "Deliberative Citizens' Assembly (survey exp)") %>%
  do(tidy(lm_robust(fair_d ~ 1, data = .))) %>% 
  filter(discussion == 0 | discussion ==1 ) %>% 
  mutate(outcome = "How Fair")

## Accept
d3 <- dat %>% 
  group_by(discussion) %>%
  filter(survey_experiment  == "Deliberative Citizens' Assembly (survey exp)") %>%
  do(tidy(lm_robust(accept_d ~ 1, data = .))) %>% 
  filter(discussion == 0 | discussion ==1 ) %>% 
  mutate(outcome = "Would Accept")

dres <- rbind(d1, d2, d3) %>% 
  mutate(condition = ifelse(discussion == 1, "Experience Treatment", "Control") )


fig2 <- dres %>% 
  ggplot(., aes(x = outcome , y = estimate, ymin = conf.low, ymax = conf.high, 
                color = condition, shape = condition,
                label = round(estimate, 2))) +
  geom_point(position=position_dodge(width = .3)) +
  geom_errorbar(width=0,position=position_dodge(width = .3)) +
  scale_colour_manual(values = c("#205C8A", "#C67800")) +
  theme_bw() +
  labs(x = "Outcome Measure (binary)", 
       y = "Proportion with Positive Response",
       color = "Condition", shape = "Condition",
       text = "Condition") +
  ylim(0, .55) +
  geom_text(position=position_dodge(width = .85), show.legend = FALSE) +
  theme(legend.position = c(.85, .9))

fig2

ggsave("figure-2.png",
       plot   = fig2,   # your ggplot or patchwork object
       width  = 6.5, height = 5, units = "in",
       dpi    = 600)





# ============================================================================ #
# Figure 3 ####


m <- dat %>% 
  filter(survey_experiment  == "Deliberative Citizens' Assembly (survey exp)") %>% 
  lm_robust(legit_index_mid01 ~ discussion + female + rural + education + age, data = . , se_type = "classical") %>% 
  tidy() %>% 
  mutate(outcome = "(a) Index",
         measure = "Continuous") %>% 
  filter(term == "discussion")

m1 <- dat %>% 
  filter(survey_experiment  == "Deliberative Citizens' Assembly (survey exp)") %>% 
  lm_robust(democratic01 ~ discussion + female + rural + education + age , data = ., se_type = "classical") %>% 
  tidy() %>% 
  mutate(outcome = "(b) How Democratic",
         measure = "Continuous") %>% 
  filter(term == "discussion")

m2 <- dat %>% 
  filter(survey_experiment  == "Deliberative Citizens' Assembly (survey exp)") %>% 
  lm_robust(dem_d ~ discussion + female + rural + education + age , data = ., se_type = "classical") %>% 
  tidy() %>% 
  mutate(outcome = "(b) How Democratic",
         measure = "Binary") %>% 
  filter(term == "discussion")

m3 <- dat %>% 
  filter(survey_experiment  == "Deliberative Citizens' Assembly (survey exp)") %>% 
  lm_robust(fair01 ~ discussion + female + rural + education + age  , data = ., se_type = "classical") %>% 
  tidy() %>% 
  mutate(outcome = "(c) How Fair",
         measure = "Continuous") %>% 
  filter(term == "discussion")


m4 <- dat %>% 
  filter(survey_experiment  == "Deliberative Citizens' Assembly (survey exp)") %>% 
  lm_robust(fair_d ~ discussion + female + rural + education + age, data = ., se_type = "classical") %>% 
  tidy() %>% 
  mutate(outcome = "(c) How Fair",
         measure = "Binary") %>% 
  filter(term == "discussion")

## Accept

m5 <- dat %>% 
  filter(survey_experiment  == "Deliberative Citizens' Assembly (survey exp)") %>% 
  lm_robust(accept01 ~ discussion + female + rural + education + age , data = ., se_type = "classical") %>% 
  tidy() %>% 
  mutate(outcome = "(d) Would Accept",
         measure = "Continuous") %>% 
  filter(term == "discussion")

m6 <- dat %>% 
  filter(survey_experiment  == "Deliberative Citizens' Assembly (survey exp)") %>% 
  lm_robust(accept_d ~ discussion + female + rural + education + age, data = ., se_type = "classical") %>% 
  tidy() %>% 
  mutate(outcome = "(d) Would Accept",
         measure = "Binary") %>% 
  filter(term == "discussion")

res <- rbind(m, m1, m2, m3, m4, m5, m6)

fig3 <- res %>% 
  ggplot(., aes(x = fct_rev(outcome), y = estimate, ymin = conf.low, ymax = conf.high, 
                color = measure, shape = measure)) +
  geom_point(position=position_dodge(width = .3)) +
  geom_errorbar(width=0,position=position_dodge(width = .3)) +
  coord_flip() +
  scale_colour_manual(values = c("#205C8A", "#C67800")) +
  theme_bw() +
  scale_y_symmetric() +
  geom_hline(yintercept = 0, linetype = 5, linewidth=.3) +
  labs(x = "Outcome Measure", 
       y = "Treatment Effect of Deliberation Experience",
       color = "Outcome Coding", shape = "Outcome Coding") 

fig3

ggsave("figure-3.png",
       plot   = fig3,   # your ggplot or patchwork object
       width  = 6.5, height = 5, units = "in",
       dpi    = 600)

# ============================================================================ #
# Figure 4 ####


pres <- dat %>% 
  mutate(priv = ifelse(privatization_dummy_b == 1, "Agrees", "Disagrees")) %>% 
  filter(survey_experiment  == "Deliberative Citizens' Assembly (survey exp)") %>% 
  group_by(priv, discussion) %>% 
  do(tidy(lm_robust(legit_index_mid01 ~ 1, data = .) )) %>% 
  filter(is.na(discussion) == FALSE) %>% 
  mutate(condition = ifelse(discussion == 1, "Experience Treatment", "Control") )


fig4 <- pres %>% 
  ggplot(., aes(x = as.factor(priv), y = estimate, ymin = conf.low, ymax = conf.high, 
                color = as.factor(condition), shape = as.factor(condition))) +
  geom_point(position=position_dodge(width = .3)) +
  geom_errorbar(width=0,position=position_dodge(width = .3)) +
  scale_colour_manual(values = c("#205C8A", "#C67800" )) +
  theme_bw() +
  labs(x = "Attitude about Privatization", 
       y = "Mean of Index",
       color = "Condition", shape = "Condition") +
  ylim(0, .6)


fig4

ggsave("figure-4.png",
       plot   = fig4,   # your ggplot or patchwork object
       width  = 6.5, height = 5, units = "in",
       dpi    = 600)


# ============================================================================ #
# Figure 5 ####

c1 <- dat %>% 
  group_by(discussion, survey_experiment) %>%
  do(tidy(lm_robust(dem_d ~ 1, data = .))) %>% 
  filter(discussion == 0 | discussion ==1 ) 

## how fair
c2 <- dat %>% 
  group_by(discussion, survey_experiment) %>%
  do(tidy(lm_robust(fair_d ~ 1, data = .))) %>% 
  filter(discussion == 0 | discussion ==1 ) 

## Accept
c3 <- dat %>% 
  group_by(discussion, survey_experiment) %>%
  do(tidy(lm_robust(accept_d ~ 1, data = .))) %>% 
  filter(discussion == 0 | discussion ==1 ) 

cres <- rbind(c1, c2, c3) %>% 
  mutate(condition = ifelse(discussion == 1, "Experience Treatment", "Control") ) %>% 
  filter(is.na(survey_experiment) == FALSE)

colors <- c("#5ec962", "#440154")


## plot
size <- 2.8
dodge <- -1

a <- cres %>% 
  filter(outcome == "dem_d") %>% 
  ggplot(., aes(x = condition , y = estimate, ymin = conf.low, ymax = conf.high, 
                color = survey_experiment, shape = survey_experiment,
                label = round(estimate,2))) +
  geom_point(position=position_dodge(width = -.3)) +
  geom_errorbar(width=0,position=position_dodge(width = -.3)) +
  scale_colour_manual(values = colors) +
  theme_bw() +
  labs(x = "Condition", 
       y = "Proportion",
       title = "(a) How Democratic",
       color = "Survey Experiment", shape = "Survey Experiment") +
  coord_cartesian(ylim = c(0, .6)) +
  geom_text(position=position_dodge(width = dodge), 
            size = size, show.legend = FALSE) 
a  

b <- cres %>% 
  filter(outcome == "fair_d") %>% 
  ggplot(., aes(x = condition , y = estimate, ymin = conf.low, ymax = conf.high, 
                color = survey_experiment, shape = survey_experiment,
                label = round(estimate,2))) +
  geom_point(position=position_dodge(width = -.3)) +
  geom_errorbar(width=0,position=position_dodge(width = -.3)) +
  scale_colour_manual(values = colors) +
  theme_bw() +
  labs(x = "Condition", 
       y = "Proportion",
       title = "(b) How Fair",
       color = "Survey Experiment", shape = "Survey Experiment") +
  coord_cartesian(ylim = c(0, .6)) +
  geom_text(position=position_dodge(width = dodge), 
            size = size, show.legend = FALSE) 
b
c <- cres %>% 
  filter(outcome == "accept_d") %>% 
  ggplot(., aes(x = condition , y = estimate, ymin = conf.low, ymax = conf.high, 
                color = survey_experiment, shape = survey_experiment,
                label = round(estimate,2))) +
  geom_point(position=position_dodge(width = -.3)) +
  geom_errorbar(width=0,position=position_dodge(width = -.3)) +
  scale_colour_manual(values = colors) +
  theme_bw() +
  labs(x = "Condition", 
       y = "Proportion",
       title = "(c) Would Accept",
       color = "Survey Experiment", shape = "Survey Experiment") +
  coord_cartesian(ylim = c(0, .6)) +
  geom_text(position=position_dodge(width = dodge), 
            size = size, show.legend = FALSE) 
c

fig5 <- a +  guide_area() + b + c  +
  plot_layout(nrow = 2, guides = 'collect') 

fig5

ggsave("figure-5.png",
       plot   = fig5,  
       width  = 6.5, height = 5, units = "in",
       dpi    = 600)

